using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Drawing;

namespace Woz
{
    public class Button
    {
        public Button(string pButtonName)
        {
            // save the name of this button into our member
            this.mName = pButtonName;

            // create the menu item
            this.mMenuItem = new ToolStripMenuItem();

            // create the toolbar button
            this.mToolStripItem = new ToolStripButton();

            // set the names on both the menu and toolstrip item
            this.mMenuItem.Name = pButtonName;
            this.mToolStripItem.Name = pButtonName;

            // set the text on the menu item and toolbar button
            this.Text = pButtonName;

            // subscribe to the click events of both the menu item and the toolstrip item
            this.mMenuItem.Click += new EventHandler(this.OnClick);
            this.mToolStripItem.Click += new EventHandler(this.OnClick);
        }

        public ToolStripMenuItem MenuItem
        {
            get
            {
                return this.mMenuItem;
            }
        }

        public ToolStripButton ToolStripItem
        {
            get
            {
                return this.mToolStripItem;
            }
        }

        public string Name
        {
            get
            {
                return this.mName;
            }
        }


        public string Text
        {
            get
            {
                // send back the text on the menu item
                return this.mMenuItem.Text;
            }
            
            set
            {
                // set the text on both the menu item and the toolbar item
                this.mMenuItem.Text = value;
                this.mToolStripItem.ToolTipText = this.mMenuItem.Text + " (" + this.mMenuItem.ShortcutKeyDisplayString + ")";
            }
        }

        public string Background
        {
            get
            {
                // merely send back the background string
                return this.mBackground;
            }

            set
            {
                Color color;
                Image newImage = null;

                // copy this into our member variable
                this.mBackground = value;

                // check if they passed in an empty string
                if (this.mBackground == null)
                {
                }

                // check if this value is a color
                else if ((color = Woz.Utilities.Instance.GetColor(value)) != Color.Empty)
                {
                    // the rectangle that well fill with the actual color
                    Rectangle rectangle = new Rectangle(1, 1, 14, 14);

                    // it is a color so lets create a new image
                    newImage = new Bitmap(16, 16);

                    // get the graphics from this image
                    using (Graphics graphics = Graphics.FromImage(newImage))
                    {
                        // draw the outline
                        graphics.Clear(Color.FromArgb(109, 109, 109));

                        // then draw the colored rectangle over it
                        using (Brush brush = new SolidBrush(color))
                        {
                            graphics.FillRectangle(brush, rectangle);
                        }
                    }
                }

                // or check if this is actually an image in the resource manager
                else if (Woz.ResourceManager.Instance.Images.ContainsKey(value))
                {
                    newImage = Woz.ResourceManager.Instance.Images[value];
                }

                this.mMenuItem.Image = newImage;
                this.mToolStripItem.Image = newImage;
            }
        }

        public string Shortcut
        {
            get
            {
                // just send back the shortcut display string
                return this.mMenuItem.ShortcutKeyDisplayString;
            }

            set
            {
                // first handle the case that its blank
                if (value == "")
                {
                    // set this on the menu item
                    this.mMenuItem.ShortcutKeys = Keys.None;

                    // change the tooltip text on the toolbar button
                    this.mToolStripItem.ToolTipText = this.mMenuItem.Text;

                    // also set this text on the menu item for future use     
                    this.mMenuItem.ShortcutKeyDisplayString = "";

                    // just leave without doing anything else
                    return;
                }

                // well use this to set the tooltip on the toolbar button
                string shortcutString = "";

                // get the parts of the string
                string[] keyStrings = value.Split(new char[]{' '});

                // well use this to create a new key in a loop then set it as the shortcut
                Keys newKey = new Keys();

                // loop through each key in the string that was sent
                foreach (string keyString in keyStrings)
                {
                    // update the tooltip shortcut string
                    shortcutString += keyString + "+";

                    // handle everything else in a smooth way
                    newKey |= (Keys)sKeysConverter.ConvertFromString(keyString);
                }
                
                // actually set the shortcut
                this.mMenuItem.ShortcutKeys = newKey;

                // fix up the tooltip just a little bit
                shortcutString = shortcutString.Replace("Control", "Ctrl");
                shortcutString = shortcutString.Remove(shortcutString.Length - 1);

                // change the tooltip text on the toolbar button
                this.mToolStripItem.ToolTipText = this.mMenuItem.Text + " (" + shortcutString + ")";
 
                // also set this text on the menu item for future use     
                this.mMenuItem.ShortcutKeyDisplayString = shortcutString;
            }
        }

        public bool Enabled
        {
            get
            {
                return this.mMenuItem.Enabled;
            }
            set
            {
                this.mMenuItem.Enabled = value;
                this.mToolStripItem.Enabled = value;
            }
        }

        public bool Checked
        {
            get
            {
                return this.mMenuItem.Checked;
            }

            set
            {
                this.mMenuItem.Checked = value;
                this.mToolStripItem.Checked = value;
            }
        }

        public bool CheckOnClick
        {
            get
            {
                return this.mMenuItem.CheckOnClick;
            }

            set
            {
                this.mMenuItem.CheckOnClick = value;
                this.mToolStripItem.CheckOnClick = value;
            }
        }

        public CheckState CheckState
        {
            get
            {
                return this.mMenuItem.CheckState;
            }

            set
            {
                this.mMenuItem.CheckState = value;
                this.mToolStripItem.CheckState = value;
            }
        }

        private void OnClick(object pSender, EventArgs pEventArgs)
        {
            if (this.Click != null)
            {
                this.Click.Invoke(this);
            }
        }

        public delegate void ButtonHandler(Woz.Button pButton);

        // the click event
        public event ButtonHandler Click;

        // the name of this button
        private string mName;

        // the name of the icon
        private string mBackground;

        // well use this to convert strings of keys to actual keys
        private static KeysConverter sKeysConverter = new KeysConverter();

        // the menu item
        private ToolStripMenuItem mMenuItem;

        // the toolbar button
        private ToolStripButton mToolStripItem;
    }
}
